package org.tgcloud.group.service.app;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.core.StringUtil;
import org.tgcloud.group.entity.MjNormalUser;
import org.tgcloud.group.entity.MjOrder;
import org.tgcloud.group.repository.MjNormalUserMapper;
import org.tgcloud.group.repository.MjOrderMapper;
import org.tgcloud.group.web.controller.dto.user.UserDTO;
import org.tgcloud.group.web.controller.req.user.UserAppSearchReq;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: zhy
 * @create: 2023-08-25 09:59
 * @description:
 */
@Service
public class NormalUserService {

    @Autowired
    private MjNormalUserMapper userMapper;

    @Autowired
    private MjOrderMapper orderMapper;

    public APPResultMessage<Object, UserDTO> getUserAppList(int userId, UserAppSearchReq req) {
        MjOrder mjOrder = orderMapper.selectByPrimaryKey(req.getOrderId());
        String pushUserIdStr = mjOrder.getPushUserIds();

        String[] split = null;
        if (StringUtil.isNotBlank(req.getNotInUserIds())){
            //转移的时候，排除一开始推送的
            if (StringUtil.isNotBlank(pushUserIdStr)){
                String[] split1 = pushUserIdStr.split(",");
                List<String> pushUserList = new ArrayList<>(Arrays.asList(split1));
                List<String> newPushUserList = Arrays.asList(req.getNotInUserIds().split(","));
                pushUserList.addAll(newPushUserList);
                split = pushUserList.toArray(new String[0]);
            }
        }

        String[] pushUsers = null;
        if (StringUtil.isNotBlank(req.getPushUserIds())){
            pushUsers = req.getPushUserIds().split(",");
        }
        MjNormalUser user = userMapper.selectByPrimaryKey(userId);
        if (user.getIsAssistant() == 2 && req.getUserType() == 1){
            if (mjOrder.getStatus() < 4 ){
                String pushUserIds = mjOrder.getPushUserIds();
                if (StringUtil.isNotBlank(pushUserIds)){
                    pushUsers = pushUserIds.split(",");
                }
                //得到转推的开拓者id
                String nextPushUserIds = mjOrder.getNextPushUserIds();
                if (StringUtil.isNotBlank(nextPushUserIds)){
                    List<String> pushUserList = new ArrayList<>(Arrays.asList(pushUsers));
                    List<String> newPushUserList = Arrays.asList(nextPushUserIds.split(","));
                    pushUserList.addAll(newPushUserList);
                    pushUsers = pushUserList.toArray(new String[0]);
                }
                //如果是助理，只可以看见所属开拓者的渠道
                List<String> a = new ArrayList();
                for (String pushUser : pushUsers) {
                    MjNormalUser mjNormalUser = userMapper.selectByPrimaryKey(userId);
                    String assistantUserIds = mjNormalUser.getAssistantUserIds();
                    for (String s : assistantUserIds.split(",")) {
                        if (pushUser.equals(s)){
                            a.add(s);
                        }
                    }
                }
                pushUsers = a.toArray(new String[0]);
            } else {

                String biddingPushUserIds = mjOrder.getBiddingPushUserIds();
                if (StringUtil.isNotBlank(biddingPushUserIds)){
                    pushUsers = biddingPushUserIds.split(",");
                }
                //如果是助理，只可以看见所属开拓者的渠道
                List<String> a = new ArrayList();
                for (String pushUser : pushUsers) {
                    MjNormalUser mjNormalUser = userMapper.selectByPrimaryKey(userId);
                    String assistantUserIds = mjNormalUser.getAssistantUserIds();
                    for (String s : assistantUserIds.split(",")) {
                        if (pushUser.equals(s)){
                            a.add(s);
                        }
                    }
                }
                pushUsers = a.toArray(new String[0]);
            }
        }

        if (req.getPageNum() != null && req.getPageSize() != null){
            PageHelper.startPage(req.getPageNum(),req.getPageSize());
            List<UserDTO> users = userMapper.selectAppListByKeyword(req.getUserType(),split,pushUsers);
            PageInfo<UserDTO> tables = new PageInfo<>(users);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, UserDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<UserDTO> users = userMapper.selectAppListByKeyword(req.getUserType(),split,pushUsers);
            ResultMessage resultMessage = ResultMessage.builder().setList(users);
            APPResultMessage<Object, UserDTO> successMessage = APPResultMessage.getSuccessMessage();
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }

    public APPResultMessage<Object, UserDTO> doSubscribeUserMsg(int userId) {
        userMapper.updateSubscribeByUserId(userId, (short) 2);
        return APPResultMessage.getSuccessMessage();
    }

    public APPResultMessage<Object, UserDTO> getUserAppListByUserType(int userId, UserAppSearchReq req) {
        String pushUserIdStr = "";
        MjOrder mjOrder = null;
        if (req.getOrderId() != null){
            mjOrder = orderMapper.selectByPrimaryKey(req.getOrderId());
            pushUserIdStr = mjOrder.getPushUserIds();
        }

        String[] split = null;
        if (StringUtil.isNotBlank(req.getNotInUserIds())){
            //转移的时候，排除一开始推送的
            if (StringUtil.isNotBlank(pushUserIdStr)){
                String[] split1 = pushUserIdStr.split(",");
                List<String> pushUserList = new ArrayList<>(Arrays.asList(split1));
                List<String> newPushUserList = Arrays.asList(req.getNotInUserIds().split(","));
                pushUserList.addAll(newPushUserList);
                split = pushUserList.toArray(new String[0]);
            }
        }

        String[] pushUsers = null;
        if (StringUtil.isNotBlank(req.getPushUserIds())){
            pushUsers = req.getPushUserIds().split(",");
        }
        MjNormalUser user = userMapper.selectByPrimaryKey(userId);
        if (user.getIsAssistant() == 2 && req.getUserType() == 1 && mjOrder != null){
            if (mjOrder.getStatus() < 4 ){
                String pushUserIds = mjOrder.getPushUserIds();
                if (StringUtil.isNotBlank(pushUserIds)){
                    pushUsers = pushUserIds.split(",");
                }
                //得到转推的开拓者id
                String nextPushUserIds = mjOrder.getNextPushUserIds();
                if (StringUtil.isNotBlank(nextPushUserIds)){
                    List<String> pushUserList = new ArrayList<>(Arrays.asList(pushUsers));
                    List<String> newPushUserList = Arrays.asList(nextPushUserIds.split(","));
                    pushUserList.addAll(newPushUserList);
                    pushUsers = pushUserList.toArray(new String[0]);
                }
                //如果是助理，只可以看见所属开拓者的渠道
                List<String> a = new ArrayList();
                for (String pushUser : pushUsers) {
                    MjNormalUser mjNormalUser = userMapper.selectByPrimaryKey(userId);
                    String assistantUserIds = mjNormalUser.getAssistantUserIds();
                    for (String s : assistantUserIds.split(",")) {
                        if (pushUser.equals(s)){
                            a.add(s);
                        }
                    }
                }
                pushUsers = a.toArray(new String[0]);
            } else {

                String biddingPushUserIds = mjOrder.getBiddingPushUserIds();
                if (StringUtil.isNotBlank(biddingPushUserIds)){
                    pushUsers = biddingPushUserIds.split(",");
                }
                //如果是助理，只可以看见所属开拓者的渠道
                List<String> a = new ArrayList();
                for (String pushUser : pushUsers) {
                    MjNormalUser mjNormalUser = userMapper.selectByPrimaryKey(userId);
                    String assistantUserIds = mjNormalUser.getAssistantUserIds();
                    for (String s : assistantUserIds.split(",")) {
                        if (pushUser.equals(s)){
                            a.add(s);
                        }
                    }
                }
                pushUsers = a.toArray(new String[0]);
            }
        }

        if (req.getPageNum() != null && req.getPageSize() != null){
            PageHelper.startPage(req.getPageNum(),req.getPageSize());
            List<UserDTO> users = userMapper.selectAppListByKeyword(req.getUserType(),split,pushUsers);
            PageInfo<UserDTO> tables = new PageInfo<>(users);
            ResultMessage result = ResultMessage.builder().setList(tables.getList()).setAllPageNumber(tables.getPages()).setCount(tables.getTotal());
            APPResultMessage<Object, UserDTO> message = APPResultMessage.getSuccessMessage();
            message.setResult(result);
            return message;
        }else {
            List<UserDTO> users = userMapper.selectAppListByKeyword(req.getUserType(),split,pushUsers);
            ResultMessage resultMessage = ResultMessage.builder().setList(users);
            APPResultMessage<Object, UserDTO> successMessage = APPResultMessage.getSuccessMessage();
            successMessage.setResult(resultMessage);
            return successMessage;
        }
    }
}
